<?php
namespace frontend\controllers;

use common\models\TSignUp;
use Yii;
use yii\web\Controller;

class ActivityController extends Controller{
    public $enableCsrfValidation = false;
    public $layout = false;

    public function actionIndex(){
        return $this->render('index');
    }

    public function actionCreate(){
        $result = ['succ' => 0, 'dataId' => 0, 'msg' => ''];
        $post = Yii::$app->request->post();
        if($post){
            if(empty($post['activityId'])){
                $post['activityId'] = 1;
            }
            $signUp = new TSignUp();
            $signUp->load($post, '');
            $signUp->status = TSignUp::STATUS_INIT;
            $signUp->_intm = time();
            $signUp->persons = intval($signUp->persons);
            if(!$signUp->persons){
                $signUp->persons = 1;
            }
            if($signUp->type == TSignUp::TYPE_TEAM && $signUp->persons < 2){
                $signUp->persons = 2;
            }
            if($signUp->save()){
                $result['succ'] = 1;
                $result['dataId'] = $signUp->id;
            }else{
                Yii::error([$post, $signUp->getErrors()], __METHOD__);
                $result['msg'] = '活动报名提交失败，请刷新后重试';
            }
        }
        return $this->asJson($result);
    }
}